package com.lailang.lailang;

import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;


public class TabbarFragment extends Fragment{

    private int tabIndex = 0;
    private ArrayList<Fragment> fragments;

    public int getTabIndex() {
        return tabIndex;
    }

    public void setTabIndex(int tabIndex) {
        if (tabIndex >= 0 && tabIndex < fragments.size()){
            this.tabIndex = tabIndex;
            Log.d("tabIndex", String.valueOf(tabIndex));


            getActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, fragments.get(tabIndex))
                    .commit();
        }

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tabbar, container, false);

        fragments = new ArrayList<>();
        fragments.add(new FirstFragment());
        fragments.add(new DaoweiFragment());
        fragments.add(new MessageFragment());
        fragments.add(new UserFragment());

        getActivity().getSupportFragmentManager().beginTransaction()
                .add(R.id.container, fragments.get(0))
                .commit();

        RadioButton rb1 = (RadioButton) rootView.findViewById(R.id.bottom_tabbar_rb_1);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            rb1.setCompoundDrawables(null, getActivity().getDrawable(R.mipmap.ic_launcher), null, null);
//        }


        RadioGroup tabGroup = (RadioGroup) rootView.findViewById(R.id.bottom_tabbar_gp);
        tabGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.bottom_tabbar_rb_1:
                        setTabIndex(0);
                        break;
                    case R.id.bottom_tabbar_rb_2:
                        setTabIndex(1);
                        break;
                    case R.id.bottom_tabbar_rb_3:
                        setTabIndex(2);
                        break;
                    case R.id.bottom_tabbar_rb_4:
                        setTabIndex(3);
                        break;
                    default:
                        break;
                }
            }
        });
        return rootView;
    }
}
